package com.openNetty.common.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 获取文件MD5工具
 *
 * @author zhenggm
 * @create 2017-11-23 上午 9:56
 **/


public class MD5Util {

    /**
     * 根据文件计算出文件的MD5
     *
     * @param file
     * @return
     */
    public static String getFileMD5(String file) {

        MessageDigest digest = null;
        MappedBiggerFileReader in = null;
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new MappedBiggerFileReader(file, 65536, 0, 0);
            while ((len = in.read()) != -1) {
                digest.update(in.getArray(), 0, len);
            }
            in.close();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());

        return bigInt.toString(16);
    }


//    public static void main(String[] args) {
//        long start = System.currentTimeMillis();
//        System.out.println(getFileMD5("E:\\1.zip"));
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//    }

}
